package com.github.mpusher.offline.httpclient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.mpusher.offline.domain.OfflineMsgVo;
import com.github.mpusher.offline.util.HttpClientUtils;
import com.mpush.api.spi.common.CacheManager;
import com.mpush.cache.redis.manager.RedisManager;
import com.mpush.tools.Jsons;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/9/25.
 */
public class PushHanlder implements IPushClient{
    @Override
    public Map<String, String> getOfflineMsg(String userId) {
        CacheManager cacheManager = RedisManager.I;
        Map<String,String> result = cacheManager.hgetAll("mp:msg:"+userId);
        return result;
    }
    @Override
    public void push(String userId,String msgId, String json) {
        String postUrl = "http://10.0.0.32:9999/push";

        OfflineMsgVo offlineMsgVo = new OfflineMsgVo();
        JSONArray jsonArray = JSON.parseArray("["+json+"]");
        Iterator<Object> iterator = jsonArray.iterator();

        while (iterator.hasNext()){
            JSONObject jsonObject = (JSONObject) iterator.next();
            if (jsonObject.getString("docUid")!=null){
                offlineMsgVo.setDocUid(jsonObject.getString("docUid"));
            }
            if (jsonObject.getString("docState")!=null){
                offlineMsgVo.setDocState(jsonObject.getString("docState"));
            }
            if (jsonObject.getString("dochandleUid")!=null){
                offlineMsgVo.setDochandleUid(jsonObject.getString("dochandleUid"));
            }
            if (jsonObject.getString("actorType")!=null){
                offlineMsgVo.setActorType(jsonObject.getString("actorType"));
            }
            if (jsonObject.getString("actorsUid")!=null){
                offlineMsgVo.setActorsUid(jsonObject.getString("actorsUid"));
            }
            if (jsonObject.getString("readStatus")!=null){
                offlineMsgVo.setReadStatus(jsonObject.getString("readStatus"));
            }
            if (jsonObject.getString("listType")!=null){
                offlineMsgVo.setListType(jsonObject.getString("listType"));
            }
            if (jsonObject.getString("title")!=null){
                offlineMsgVo.setTitle(jsonObject.getString("title"));
            }
        }
        //设置请求参数
        Map<String, Object> params = new HashMap<String,Object>();
        params.put("users",userId);
        params.put("docUid",userId);
        params.put("docState",userId);
        params.put("dochandleUid",userId);
        params.put("actorType",userId);
        params.put("actorsUid",userId);
        params.put("readStatus",userId);
        params.put("title",userId);
        params.put("msgId",msgId);

        String data = Jsons.toJson(params);

        HttpClientUtils.sendData(data,postUrl,null,"application/json");
    }

    public static void main(String[] args) {
        PushHanlder pushHanlder = new PushHanlder();
    }
}
